﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestionePolveriLib.Facades;
using GestionePolveriLib.BL;

namespace GestionePolveri2
{
    public partial class LoginForm : Form
    {
        public event EventHandler ApplicationShutdown;

        Operatore m_operatore = null;
        private bool m_closeWindow = true;

        public Operatore Operatore
        {
            get { return m_operatore; }
        }

        public LoginForm()
        {
            InitializeComponent();

            this.FormClosing += new FormClosingEventHandler(LoginForm_FormClosing);
        }

        void Instance_DataLoaded(object sender, EventArgs e)
        {
            this.Close();           
        }

        void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !m_closeWindow;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            OperatoriFacade opf = new OperatoriFacade();
            if (opf.Validate(this.txtUsername.Text, this.txtPassword.Text))
            {
                m_operatore = opf.GetOperatore(this.txtUsername.Text); 

                string[] roles = RuoliFacade.GetRuoliOperatore(this.txtUsername.Text);
                System.Security.Principal.IIdentity identity = new System.Security.Principal.GenericIdentity(this.txtUsername.Text);
                //System.Security.Principal.IPrincipal principal = new System.Security.Principal.GenericPrincipal(identity, roles);

                OperatorePrincipal m_operatorePrincipal = new OperatorePrincipal(identity, roles);
                m_operatorePrincipal.Operatore = m_operatore;

                System.Security.Principal.IPrincipal principal = m_operatorePrincipal;
                
                System.Threading.Thread.CurrentPrincipal = principal;

                this.DialogResult = System.Windows.Forms.DialogResult.OK;

                m_closeWindow = true;
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                MessageBox.Show("Credenziali di accesso errate", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Error);

                m_closeWindow = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtPassword.Clear();
            this.txtUsername.Clear();
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOk_Click(sender, new EventArgs());
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (ApplicationShutdown != null)
            {
                ApplicationShutdown(this, EventArgs.Empty);
            }
        }
    }
}
